//
//  MainMenuScene.m
//  iFly
//
//  Created by User-23 on 12/8/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "MainMenuScene.h"
#import "GameScene.h"
#import "Constants.h"
#import "LeaderboardScene.h"
#import "SoundHandler.h"
#import "TutorialScene.h"

@interface MainMenuScene ()
@property (strong,nonatomic) IBOutletCollection(SKLabelNode) NSMutableArray* labels;
@property SKSpriteNode* pic;
@property SKSpriteNode* background;

@end


@implementation MainMenuScene
static float scaleMenuCoeficient;

-(instancetype)initWithSize:(CGSize)size {
    if(self = [super initWithSize:size]) {
        self.labels = [[NSMutableArray alloc]init];
        
        [[SoundHandler sharedSoundPlayer] playBackgroundMusicWithURL:mainMenuSound andExtension:@"mp3"];
        
        scaleMenuCoeficient=size.height/640;
        self.background= [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"amazing clouds"]];
        self.background.position = CGPointZero;
        self.background.anchorPoint = CGPointZero;
        [self addChild:self.background];
        
        //setting logo
        SKLabelNode* label = [SKLabelNode labelNodeWithFontNamed:labelFontName];
        label.text = logoLabelName;
        label.fontSize = 60;
        label.fontColor = [SKColor purpleColor];
        label.position = CGPointMake(self.size.width/2, self.size.height/1.25);
        [label setScale:scaleMenuCoeficient];
        [self addChild:label];
        
        //setting (un)masked picture of character
        self.pic = [SKSpriteNode spriteNodeWithImageNamed:kImageNameForIronBaseImage];
        self.pic.position = CGPointMake(self.size.width/ironManPicWidthRetreat, self.size.height/ironManPicHeightRetreat);
        [self.pic setScale:scaleMenuCoeficient];
        [self addChild:self.pic];
        

        //setting play game button
        SKLabelNode* playButton = [SKLabelNode labelNodeWithFontNamed:labelFontName];
        playButton.text = playLabelName;
        playButton.position = CGPointMake(self.size.width-retreatRightBorder*scaleMenuCoeficient, self.size.height/1.3);
        [self.labels addObject:playButton];
        
        
        //setting tutorial button
        SKLabelNode* tutorialButton = [SKLabelNode labelNodeWithFontNamed:labelFontName];
        tutorialButton.text = tutorialLabelName;
        tutorialButton.position = CGPointMake(self.size.width-retreatRightBorder*scaleMenuCoeficient, self.size.height/1.9);
        [self.labels addObject:tutorialButton];
        
        
        //setting high score button
        SKLabelNode* highScoreButton = [SKLabelNode labelNodeWithFontNamed:labelFontName];
        highScoreButton.text = highScoreLabelName;
        highScoreButton.position = CGPointMake(self.size.width-retreatRightBorder*scaleMenuCoeficient, self.size.height/3.8);
        [self.labels addObject:highScoreButton];
        
        for (SKLabelNode* label in self.labels) {
            label.name = label.text;
            label.fontColor = [SKColor blackColor];
            [label setScale:scaleMenuCoeficient];
            [self addChild:label];
        }
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    
    if ([node.name isEqualToString:playLabelName]) {
        [[SoundHandler sharedSoundPlayer] playSoundWithName:@"helmetSound" andType:@"wav"];
                
        NSMutableArray* textures = [[NSMutableArray alloc]init];
        for (int idx=1; idx < numberOfIronPicImages; idx++) {
            NSString* textureName = [NSString stringWithFormat:@"%@0%d",kImageNameForIronBaseImage,idx];
            SKTexture* texture = [SKTexture textureWithImageNamed:textureName];
            [textures addObject:texture];
        }
        SKAction* move = [SKAction animateWithTextures:textures timePerFrame:1.5];
        SKAction* rest = [SKAction setTexture:[textures objectAtIndex:[textures count]-1]];
        
        SKAction* presentGameScene = [SKAction runBlock:^{
            SKTransition *reveal = [SKTransition flipHorizontalWithDuration:1.0];
            GameScene * scene = [GameScene sceneWithSize:self.view.bounds.size];
            scene.scaleMode = SKSceneScaleModeAspectFill;
            [self.view presentScene:scene transition:reveal];
        }];
        SKAction* sequence = [SKAction sequence:@[move,rest,presentGameScene]];
        [self.pic runAction:sequence];
        
    }
    SKTransition * reveal=[SKTransition doorsOpenVerticalWithDuration:0.5];
    if ([node.name isEqualToString:highScoreLabelName]) {
        LeaderboardScene* leaderboard=[[LeaderboardScene alloc] initWithSize:self.view.bounds.size andPreviousScene:mainMenuLabelName andScore:0];
        leaderboard.scaleMode=SKSceneScaleModeAspectFill;
        [self.view presentScene:leaderboard transition:reveal];
    }
    
    if([node.name isEqualToString:tutorialLabelName]){
        TutorialScene* tutorial = [[TutorialScene alloc]initWithSize:self.view.bounds.size andPreviousScene:mainMenuLabelName];
        tutorial.scaleMode = SKSceneScaleModeAspectFill;
        [self.view presentScene:tutorial transition:reveal];
    }
}
@end
